﻿//此文件用于查找明场图中关于损伤的数据
#include <opencv2/opencv.hpp>
#include <thread>
#include <stack>
#include <unordered_map>
#include <map>
#include <ctime>
#include <filesystem>
#include "public.h"
#include "FinderData.hpp"

std::vector<std::string> args;
FinderData finderData;
cv::Mat color_mark;//图片颜色标记png


struct damage {
    uint32_t ref{0};
    uint32_t MIN_x{UINT32_MAX};
    uint32_t MAX_x{0};
    uint32_t MIN_y{UINT32_MAX};
    uint32_t MAX_y{0};
    uint32_t area{0};
    damage(uint32_t _ref ) : ref(_ref) {

    }
    damage() {
    }
};

std::map<uint16_t, damage> refMapDamage;


int main(int argn, const char **argv) {
    printf("//处理参数列表\r\n");
    if (argn == 1) {
        return 0;
    } else if (strcmp(argv[1], "--test") == 0) {
        args.emplace_back(R"(C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\FinderWorkSpace.png)");
    } else {
        for (int i = 1; i < argn; ++i) {
            args.emplace_back(argv[i]);
        }
    }
    printf("//加载颜色标记以及xlsx\r\n");
    color_mark = cv::imread(GetFilePath(args[0]) + "FinderWorkSpace.png", cv::IMREAD_UNCHANGED);
    finderData.open(GetFilePath(args[0]) + "FinderWorkSpace.xlsx");
    printf("现在一共有%d个损伤点记录\r\n", finderData.size());
    if (color_mark.empty() || finderData.size() == 0) {
        printf("//无效文件\r\n");
        return 0;
    }
    for (int i = 0; i < finderData.size(); ++i) {
        refMapDamage[finderData("编号", i).value().get<uint16_t>()]
                = damage(finderData("编号", i).value().get<uint16_t>());
    }

    for (int r = 0; r < color_mark.rows; ++r) {
        uint16_t *pix_color_mark = (uint16_t *) ((size_t) color_mark.data + r * color_mark.step);
        for (int c = 0; c < color_mark.cols; ++c) {
            if (*pix_color_mark != 0) {
                auto dam = refMapDamage.find(*pix_color_mark);
                if (dam != refMapDamage.end()) {
                    dam->second.area++;
                    dam->second.MIN_x = MIN(dam->second.MIN_x, c);
                    dam->second.MAX_x = MAX(dam->second.MAX_x, c);
                    dam->second.MIN_y = MIN(dam->second.MIN_y, r);
                    dam->second.MAX_y = MAX(dam->second.MAX_y, r);
                } else {
                    printf("警告，存在png中而xlsx不存在的编号%d\r\n", *pix_color_mark);
                }
            }
            pix_color_mark++;
        }
    }

    for (auto i = refMapDamage.begin(); i != refMapDamage.end(); i++) {
        finderData("x", finderData.idxOFref(i->second.ref)).value() = i->second.MIN_x;
        finderData("y", finderData.idxOFref(i->second.ref)).value() = i->second.MIN_y;
        finderData("当量", finderData.idxOFref(i->second.ref)).value() = i->second.area;
        auto w = i->second.MAX_x - i->second.MIN_x + 1;
        auto h = i->second.MAX_y - i->second.MIN_y + 1;
        if (w > 100 || h > 100) {
            printf("ref %d,w= %d,h= %d\r\n", i->second.ref, w, h);
        }
        finderData("w", finderData.idxOFref(i->second.ref)).value() = w;
        finderData("h", finderData.idxOFref(i->second.ref)).value() = h;

    }
    finderData.save();
    system("pause");
    return 0;


}